package lol.clann.bukkit.pluginboot.config.jackson.block;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lol.clann.bukkit.pluginboot.util.BlockUtils;
import lol.clann.bukkit.pluginboot.util.TileEntityUtils;
import lol.clann.bukkit.pluginboot.util.WorldUtils;
import lol.clann.clannboot.annotation.inject.Inject;
import lol.clann.clannboot.annotation.svc.PostInstance;
import lol.clann.clannboot.annotation.svc.Service;
import lol.clann.clannboot.config.jackson.Jackson;
import org.bukkit.block.Block;

import java.io.IOException;

/**
 * Block只有序列化,没有反序列化
 *
 * @author pyz
 * @date 2018/10/12 下午10:38
 */
@Service(priority = Integer.MAX_VALUE - 100)
public class BlockRegister {
    @Inject
    private WorldUtils worldUtils;
    @Inject
    private BlockUtils blockUtils;
    @Inject
    private TileEntityUtils tileEntityUtils;

    @PostInstance
    private void init() {
        Jackson.addSerializer(Block.class, new JsonSerializer<Block>() {
            @Override
            public void serialize(Block value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
                Object tag = blockUtils.toCompound(value);
                gen.writeObject(tag);
            }
        });
    }
}
